﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using YXShop.Model.Order;

namespace YXShop.Web.admin.include
{
    public partial class usershoppinginfo : System.Web.UI.Page
    {
        int uid =0;
        YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                if (Request.Form["Option"] != null && !Request.Form["Option"].Trim().Equals(""))
                {
                    string types = Request.Form["Option"].Trim();
                    if (types == "Save")
                    {
                        uid = Convert.ToInt32(HttpContext.Current.Session["MemberID"]);
                        string str = this.SaveInfo();
                        Response.Write(str);
                    }
                    if (types == "Province")
                    {
                        string ProvinceId = Request.Form["ProvinceId"].Trim();
                        Response.Write(YXShop.Common.Province.Provinces_City2("", ProvinceId));
                    }
                    if (types == "Citys")
                    {
                        string CitysId = Request.Form["CitysId"].Trim();
                        Response.Write(YXShop.Common.Province.Provinces_Borough2("", CitysId));
                    }
                    Response.End();
                    return;
                }
                this.Initialize();
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void Initialize()
        {
            string consigneInfo = ChangeHope.WebPage.PageRequest.GetQueryString("userid");
            string[] strConsigneInfo = consigneInfo.Split('|');
            this.ConsigneID.Value = strConsigneInfo[1];
            this.PutoutTypeID.Value = strConsigneInfo[2];
            this.PutoutID.Value = strConsigneInfo[3];
            uid = Convert.ToInt32(HttpContext.Current.Session["MemberID"]);
            string UID = string.Empty;
            string UserName = string.Empty;
            string AddressInfo = string.Empty;
            string Tel = string.Empty;
            string Fax = string.Empty;
            string post = string.Empty;
            string HomePhone = string.Empty;
            string Email = string.Empty;
            string Province = string.Empty;
            string City = string.Empty;
            string Borough = string.Empty;
            string constructionSigns = string.Empty;
            string consignesTime = string.Empty;
            if (strConsigneInfo[1] != "")
            {
                YXShop.BLL.Member.ReceAddress rabll = new YXShop.BLL.Member.ReceAddress();
                YXShop.Model.Member.ReceAddress ramodle = rabll.GetModelByID(Convert.ToInt32(strConsigneInfo[1]));
                if (ramodle != null)
                {
                    UID = ramodle.UID.ToString();
                    UserName = ramodle.UserName;
                    AddressInfo = ramodle.Address;
                    HomePhone = ramodle.Phone;
                    Fax = ramodle.Fax;
                    post = ramodle.Zip;
                    Tel = ramodle.Mobile;
                    Email = ramodle.Email;
                    Province = ramodle.Province;
                    City = ramodle.City;
                    Borough = ramodle.Borough;
                    constructionSigns = ramodle.ConstructionSigns;
                    consignesTime = ramodle.ConsignesTime;

                }
            }
            else
            {
                YXShop.BLL.Member.MemberInfo mibll = new YXShop.BLL.Member.MemberInfo();
                YXShop.Model.Member.MemberInfo mimodel = mibll.GetModel(uid);
                if (mimodel != null)
                {
                    UID = mimodel.UID.ToString();
                    UserName = mimodel.TrueName;
                    AddressInfo = mimodel.Address;
                    HomePhone = mimodel.HomePhone;
                    Fax = mimodel.Fax;
                    post = mimodel.Zip;
                    Tel = mimodel.HandPhone;
                    Email = mimodel.MSN;
                    Province = mimodel.Province;
                    City = mimodel.City;
                    Borough = mimodel.Borough;
                }
            }
            
            this.userName.Value = UserName;
            this.userAddr.Value = AddressInfo;
            this.userPhone.Value = HomePhone;
            this.userZip.Value = post;
            this.userMobile.Value = Tel;
            this.userEmail.Value = Email;
            StringBuilder shtml = new StringBuilder();
            shtml.Append("<span>" + YXShop.Common.Province.Provincess2(Province) + "</span><span id=\"Cityli\">&nbsp;" + YXShop.Common.Province.Provinces_City2(City, Province) + "</span><span id=\"City2li\">&nbsp;" + YXShop.Common.Province.Provinces_Borough2(Borough, City) + "</span> ");
            this.Literal1.Text = shtml.ToString();
        }

        private string SaveInfo()
        {
            uid = Convert.ToInt32(HttpContext.Current.Session["MemberID"]);
            string consigneInfoId = ChangeHope.WebPage.PageRequest.GetFormString("ConsigneInfoId");
            YXShop.BLL.Member.ReceAddress bll = new YXShop.BLL.Member.ReceAddress();
            YXShop.Model.Member.ReceAddress member = new YXShop.Model.Member.ReceAddress();
            member.UserName = ChangeHope.WebPage.PageRequest.GetFormString("userName");
            member.Address = ChangeHope.WebPage.PageRequest.GetFormString("userAddr");
            member.Mobile = ChangeHope.WebPage.PageRequest.GetFormString("userMobile");
            member.Fax = "";
            member.Zip = ChangeHope.WebPage.PageRequest.GetFormString("userZip");
            member.Email = ChangeHope.WebPage.PageRequest.GetFormString("userEmail");
            member.Phone = ChangeHope.WebPage.PageRequest.GetFormString("userHome");
            member.City = ChangeHope.WebPage.PageRequest.GetFormString("CitysId");
            member.Province = ChangeHope.WebPage.PageRequest.GetFormString("ProvinceId");            
            member.Borough = ChangeHope.WebPage.PageRequest.GetFormString("BoroughId");
            member.ConsignesTime = ChangeHope.WebPage.PageRequest.GetFormString("ConsigneTime");
            member.ConstructionSigns = ChangeHope.WebPage.PageRequest.GetFormString("ConstructionSigns");
            string userTypeId = ChangeHope.WebPage.PageRequest.GetFormString("PutoutTypeId");
            string userId = ChangeHope.WebPage.PageRequest.GetFormString("PutoutId");
            //如果用户为登陆，添加收货信息表
            string operate = "<a href=\"javascript:showPath('UserShoppingInfo',document.getElementById('address'),'点击修改收货人信息息',700,285,'" + sp.DummyPaht + "','0||" + userTypeId + "|" + userId + "')\" title=\"点击修改收货人信息息\"><span style='color:#0000FF'>点击修改收货人信息息</span></a>";
            if (uid > 0)
            {
                member.UID = uid;
                member.UserId = HttpContext.Current.Session["MemberName"].ToString();
                member.Stat = 1;
                if (consigneInfoId != "")
                {
                    member.ID = Convert.ToInt32(consigneInfoId);
                    bll.Amend(member);
                    operate = "<a href=\"javascript:showPath('UserShoppingInfo',document.getElementById('address'),'点击修改收货人信息息',700,285,'" + sp.DummyPaht + "','0||" + userTypeId + "|" + userId + "')\" title=\"点击修改收货人信息息\"><span style='color:#0000FF'>点击修改收货人信息息</span></a>";
                }
                else
                {
                    int conId=bll.Add(member);
                    operate = "<a href=\"javascript:showPath('UserShoppingInfo',document.getElementById('address'),'修改收货人信息',700,285,'" + sp.DummyPaht + "','" + uid + "|" + conId + "|" + userTypeId + "|" + userId + "')\" title=\"点击修改收货人信息息\"><span style='color:#0000FF'>点击修改收货人信息息</span></a>";
                    
                }
            }

            string reStr = string.Empty;
            
            StringBuilder shtml = new StringBuilder();
            shtml.Append("<table class=\"ConsignesTable\"   align=\"center\" border=\"0\" cellpadding=\"5\" cellspacing=\"1\">");
            shtml.Append("<tr>");
            shtml.Append("<td class=\"ConsignesTd_Name\">收货人姓名：</td>");
            shtml.Append("<td class=\"ConsignesTd_Value\"><input type=\"text\" name=\"userName\" id=\"userName\" readonly value=\"" + ChangeHope.WebPage.PageRequest.GetFormString("userName") + "\"></td>");
            shtml.Append("<td class=\"ConsignesTd_Name\">电子邮件地址：</td>");
            shtml.Append("<td class=\"ConsignesTd_Value\"><input type=\"text\" name=\"userEmail\" id=\"userEmail\" readonly value=\"" + ChangeHope.WebPage.PageRequest.GetFormString("userEmail") + "\"></td>");
            shtml.Append("</tr>");
            shtml.Append("<tr>");
            shtml.Append("<td class=\"ConsignesTd_Name\">详细地址：</td>");
            shtml.Append("<td class=\"ConsignesTd_Value\"><input type=\"text\" name=\"userAddr\" id=\"userAddr\" readonly value=\"" + ChangeHope.WebPage.PageRequest.GetFormString("userAddr") + "\"></td>");
            shtml.Append("<td class=\"ConsignesTd_Name\">邮政编码：</td>");
            shtml.Append("<td class=\"ConsignesTd_Value\"><input type=\"text\" name=\"userZip\" id=\"userZip\" readonly value=\"" + ChangeHope.WebPage.PageRequest.GetFormString("userZip") + "\"></td>");
            shtml.Append("</tr>");
            shtml.Append("<tr>");
            shtml.Append("<td class=\"ConsignesTd_Name\">电话：</td>");
            shtml.Append("<td class=\"ConsignesTd_Value\"><input type=\"text\" name=\"userPhone\" id=\"userPhone\" readonly value=\"" + ChangeHope.WebPage.PageRequest.GetFormString("userHome") + "\"></td>");
            shtml.Append("<td class=\"ConsignesTd_Name\">手机：</td>");
            shtml.Append("<td class=\"ConsignesTd_Value\"><input type=\"text\" name=\"userMobile\" id=\"userMobile\" readonly value=\"" + ChangeHope.WebPage.PageRequest.GetFormString("userMobile") + "\"></td>");
            shtml.Append("</tr>");
            shtml.Append("<tr>");
            shtml.Append("<td class=\"ConsignesTd_Name\">标志建筑：</td>");
            shtml.Append("<td class=\"ConsignesTd_Value\"><input type=\"text\" name=\"ConstructionSigns\" id=\"ConstructionSigns\" readonly value=\"" + ChangeHope.WebPage.PageRequest.GetFormString("ConstructionSigns") + "\"></td>");
            shtml.Append("<td class=\"ConsignesTd_Name\">最佳送货时间：</td>");
            shtml.Append("<td class=\"ConsignesTd_Value\"><input type=\"text\" name=\"ConsigneTime\" id=\"ConsigneTime\" readonly value=\"" + ChangeHope.WebPage.PageRequest.GetFormString("ConsigneTime") + "\"></td>");
            shtml.Append("</tr>");
            shtml.Append("<tr><td colspan=\"4\" class=\"ConsignsOperate\"><input type=\"hidden\" name=\"isFillConsignedata\" id=\"isFillConsignedata\" value=\"1\" />" + operate + "</td></tr>");
            shtml.Append("</table>");
            shtml.Append("partpartpart");
            shtml.Append(this.CsharpVoid(member.Province,member.City,member.Borough));
            reStr = shtml.ToString();
            return reStr;
        }

        protected string CsharpVoid(string provinces, string city, string Borough)
        {
            YXShop.BLL.SystemInfo.PostArea postarea = new YXShop.BLL.SystemInfo.PostArea();
            List<YXShop.Model.SystemInfo.PostArea> list = null;
            YXShop.BLL.SystemInfo.Deliver deliver = new YXShop.BLL.SystemInfo.Deliver();
            YXShop.Model.SystemInfo.Deliver objdeli = null;
            string str = string.Empty;
            StringBuilder shtml = new StringBuilder();
            string UserTypeId = ChangeHope.WebPage.PageRequest.GetFormString("PutoutTypeId");//收货会员类型
            string UserId = ChangeHope.WebPage.PageRequest.GetFormString("PutoutId");//添加者ID
            //如果支付给网站运营商：
            if (sp.IsPayMentWebOperation)
            {
                if (Borough != string.Empty && Borough != "")
                {
                    list = postarea.GetPostMethodByAreaId(Borough, "  putouttyid=0");
                    if (list.Count == 0)
                    {
                        list = postarea.GetPostMethodByAreaId(city, " putouttyid=0");
                        if (list.Count == 0)
                        {
                            list = postarea.GetPostMethodByAreaId(provinces, " putouttyid=0");
                        }
                    }

                }
            }
            else
            {
                if (Borough != string.Empty && Borough != "")
                {
                    if (UserTypeId == "0")
                    {
                        list = postarea.GetPostMethodByAreaId(Borough, "  putouttyid=0");
                        if (list.Count == 0)
                        {
                            list = postarea.GetPostMethodByAreaId(city, " putouttyid=0");
                            if (list.Count == 0)
                            {
                                list = postarea.GetPostMethodByAreaId(provinces, " putouttyid=0");
                            }
                        }
                    }
                    else
                    {
                        list = postarea.GetPostMethodByAreaId(Borough, "  putouttyid=" + UserTypeId + " and putoutid=" + UserId + " ");
                        if (list.Count == 0)
                        {
                            list = postarea.GetPostMethodByAreaId(city, " putouttyid=" + UserTypeId + " and putoutid=" + UserId + "");
                            if (list.Count == 0)
                            {
                                list = postarea.GetPostMethodByAreaId(provinces, " putouttyid=" + UserTypeId + " and putoutid=" + UserId + "");
                            }
                        }
                    }
                }
            }

            shtml.Append("<div id=\"divDeliveryMode\">");
            shtml.Append("<table id=\"DeliveryModeTable\" class=\"DeliveryModeTable\" align=\"center\" border=\"0\" cellpadding=\"5\" cellspacing=\"1\">");
            shtml.Append("<tr>");
            shtml.Append("<th class=\"DeliveryModeTitle1\"></th>");
            shtml.Append("<th class=\"DeliveryModeTitle2\">名称</th>");
            shtml.Append("<th class=\"DeliveryModeTitle3\">订购描述</th>");
            shtml.Append("<th class=\"DeliveryModeTitle4\">费用</th>");
            shtml.Append("<th class=\"DeliveryModeTitle5\">免费额度</th>");
            shtml.Append("<th class=\"DeliveryModeTitle6\">保价费用</th>");
            shtml.Append("</tr>");
            ProfileCommon Profile = new ProfileCommon();
            ICollection<CartItemInfo> cart = Profile.ShoppingCart.CartItems;
            double productTotalPrice = 0;
            double productTotalWeight = 0;
            int Pro_TotalProductCount = 0;
            YXShop.BLL.Product.ProductInfo productbll = new YXShop.BLL.Product.ProductInfo();
            YXShop.TemplateAction.Common dp = new YXShop.TemplateAction.Common();
            foreach (CartItemInfo info in cart)
            {
                YXShop.Model.Product.ProductInfo productmodel = productbll.GetModel(int.Parse(info.ProductId));
                if (productmodel != null)
                {
                    double price = 0.0;
                    price = double.Parse(productmodel.pro_ShopPrice.ToString());
                    if (HttpContext.Current.Session["MemberID"] != null)
                    {
                        price =YXShop.Common.ProductInfo.DiscountedPrice(Convert.ToInt32(HttpContext.Current.Session["MemberID"].ToString()), price, productmodel.pro_RatingDiscount);
                    }
                    double count = 0;
                    count = double.Parse(info.Quantity.ToString());
                    double total = price * count;
                    productTotalPrice += total;
                    Pro_TotalProductCount += Convert.ToInt32(count);
                    productTotalWeight += Convert.ToDouble(productmodel.pro_Weight);
                }
            }
            //如果有配送区域的数据
            if (list.Count > 0)
            {
                foreach (YXShop.Model.SystemInfo.PostArea objpost in list)
                {
                    objdeli = deliver.GetModelByID(objpost.Deliverymode);
                    string delivery = YXShop.Common.OrderInfo.TotalFreight(Convert.ToInt32(objpost.Id), float.Parse(productTotalWeight.ToString()), float.Parse(productTotalPrice.ToString()), Pro_TotalProductCount);
                    shtml.Append("<tr>");
                    shtml.Append("<td><input type=\"radio\" id=\"DeliveryMode\" name=\"DeliveryMode\" value=\"" + objpost.Id.ToString() + "\" onclick=\"PointCost('" + delivery + "','freight')\" /></td>");
                    shtml.Append("<td>" + objdeli.Distributionname + "</td>");
                    shtml.Append("<td>" + objdeli.Distributiondescription + "</td>");
                    shtml.Append("<td>￥" + delivery + "元</td>");
                    shtml.Append("<td>￥" + Convert.ToDouble(objpost.Freeamount).ToString("f2") + "元</td>");
                    string insuredCosts = objdeli.Insuredcosts.ToString();
                    if (insuredCosts == "0")
                    {
                        insuredCosts = "不支持保价";
                    }
                    else
                    {
                        insuredCosts = objdeli.Insuredcosts.ToString() + "%";
                    }
                    shtml.Append("<td>" + insuredCosts + "</td>");
                    shtml.Append("</tr>");
                }
            }
            shtml.Append("</table>");
            shtml.Append("</div>");
            str = shtml.ToString();
            return str;
        }

    }
}
